<template>
	<view>
		<scroll-view scroll-x class="bg-white nav">
			<view class="flex text-center">
				<view class="cu-item flex-sub" :class="index==TabCur?'text-orange cur':''" v-for="(item,index) in tabList" :key="index" @tap="tabSelect" :data-id="index">
					{{item}}
				</view>
			</view>
		</scroll-view>
		<uni-clientdb ref="udb" v-slot:default="{data, loading, error, options}" :options="formData" :collection="collection"
		 :field="field" @load="load" orderby="date desc" :where="dbWhere" v-if="uid">
			<view class="cu-list menu-avatar">
				<view class="cu-item" v-for="item in data" :key="item._id">
					<view class="cu-avatar round lg bg-brown" :style="'background-image:url('+item.giftInfo.imgPath[0]+');'">
					</view>
					<view class="content">
						<view class="text-grey">{{item.giftInfo.title}}|{{item.giftInfo.attr[item.giftInfo.AttrIndex]}}</view>
						<view class="text-gray text-sm flex">
							<uni-dateformat :date="item.create_date" ></uni-dateformat>
						</view>
					</view>
				</view>
			</view>
			<uni-load-more v-if="loading || options.status === 'noMore' " :status="options.status" />
		</uni-clientdb>
	</view>
</template>

<script>
	const app = getApp();
	const db = uniCloud.database();
	const _ = db.command;
	
	export default {
		data() {
			return {
				collection: 'giftOrders',
				field: '_id, send, uid, create_date, giftInfo',
				formData: {
					status: 'loading', // 加载状态
				},
				uid: null,
				TabCur: 0,
				tabList: ['全部', '已支付', '已签收'],
				dbWhere: ""
			}
		},
		
		onLoad() {
			if (!app.globalData.hasLogin) {
				uni.showModal({
					title: "请先登录",
					mask: true,
					success: (res) => {
						if(res.confirm){
							uni.switchTab({
								url:"/pages/profile/profile"
							})
						} else {
							uni.navigateBack({
								delta: 1
							})
						}
					}
				});
			}
			this.uid = app.globalData.userInfo._id;
		},
		
		methods: {
			tabSelect(e) {
				this.TabCur = e.currentTarget.dataset.id;
				if (this.TabCur == 0) {
					this.dbWhere = {
						uid: this.uid
					};
				} else if (this.TabCur == 1) {
					this.dbWhere = {
						uid: this.uid,
						send: false
					};
				} else if (this.TabCur == 2) {
					this.dbWhere = {
						uid: this.uid,
						send: true
					};
				}
				this.scrollLeft = (e.currentTarget.dataset.id - 1) * 60
			},
			/**
			 * 下拉刷新回调函数
			 */
			onPullDownRefresh() {
				this.tipShow = true
				this.formData.status = 'more'
				this.$refs.udb.loadData({
					clear: true
				}, () => {
					this.tipShow = false
					uni.stopPullDownRefresh()
				})
			},
			/**
			 * 上拉加载回调函数
			 */
			onReachBottom() {
				this.$refs.udb.loadMore()
			},
			
			load(data,ended){
				if(ended){
					this.formData.status = 'noMore'
				}
			}
		}
	}
</script>

<style>

</style>
